<?php
namespace Home\Controller;
class WorkorderController extends CommonController
{
	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("work");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData', session('heardadminData'));
	}

	//工单列表
	public function workorder_list()
	{
		$this->p = I('get.p')?I('get.p'):1;
		$keyword = I('get.keyword');
		$state = I('get.state');
		$department = I('get.department');
		$this->keyword = $keyword;
		$this->state = $state;
		$this->department = $department;
		$where = array();
		if($keyword){
			$where['workorder_name'] = array('like',"%$keyword%");
		}
		if($state){
			$where['workorder_status'] = $state;
		}
		if($department){
			if($department==1){//$department为1时 为管理员（0管理员 3市场部 4运营部）
				$where['department'] = 0;
			}else{
				$where['department'] = $department;
			}

		}
		$count = M('workorder')->where($where)->count();

		$p = getpage($count, 20);
		$workorder_list = M('workorder o')
			->field('o.*,a.heard_adminname as nickname,b.heard_adminname as confirm_name')
			->where($where)
			->join('__ADMIN__ a on a.heard_admin_id = o.create_by','left')
			->join('__ADMIN__ b on b.heard_admin_id = o.confirm_id','left')
			->limit($p->firstRow, $p->listRows)
			->order('add_time DESC')
			->select();
		foreach($workorder_list as &$v){
			$v['confirm_time'] = $v['confirm_time']?date('Y-m-d H:i:s',$v['confirm_time']):'';
			$v['remark_time'] = $v['remark_time']?date('Y-m-d H:i:s',$v['remark_time']):'';
		}
		$page = $p->show();

		$map['keyword'] = empty($keyword)?"":$keyword;
		$map['state'] = empty($state)?"":$state;
		$map['department'] = empty($department)?"":$department;
		//分页参数
		$p->parameter = "";
		foreach($map as $key=>$val) {
			$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->assign('workorder_list',$workorder_list);
		$this->assign('page', $page);
		$this->display();
	}

	//创建工单
	public function workorder_add(){
		if(IS_POST){
			$data = I('post.');
			empty($data['workorder_name'])?$this->error("工单名称不能为空！"):$name = $data['workorder_name'];
			empty($data['workorder_content'])?$this->error("工单内容不能为空！"):$content = $data['workorder_content'];
			(!in_array($data['department'],[0,3,4]))?$this->error("指定部门参数错误！"):$department = $data['department'];
			$add['workorder_name'] = $name;
			$add['workorder_content'] = $content;
			$add['department'] = $department;
			$add['workorder_status'] = 1;
			$add['add_time'] = date('Y-m-d H:i:s',time());
			$add['create_by'] =urlsafe_b64decode(cookie("heardadminData"));
			$res = M('workorder')->add($add);
			if($res){
				$this->adminlog("创建工单".$res);
				$this->success('创建成功',"/Home/Workorder/workorder_list",1);
			}else{
				$this->error('创建失败');
			}
		}else{
			$this->display();
		}
	}

	//工单确认
	public function order_confirm(){
		$workorder_id = I('get.workorder_id',0,'intval');
		$workorder_info = M('workorder')->where("id = $workorder_id")->find();
		if($workorder_info && ($workorder_info['department']==cookie('founder') || cookie('founder') == 1)){
			if($workorder_info['workorder_status'] != 1){
				$this->error('订单已确认过了！');
			}
			$adminid = urlsafe_b64decode(cookie("heardadminData"));
			$save['workorder_status'] = 2;
			$save['confirm_id'] = $adminid;
			$save['confirm_time'] = time();
			$res = M('workorder')->where(['id'=>$workorder_id])->save($save);
			if($res){
				$this->adminlog("确认工单".$workorder_id);
				$this->success('确认成功','/Home/Workorder/workorder_list',1);
			}else{
				$this->error('确认失败');
			}
		}else{
			$this->error('工单不存在或无权处理该订单');
		}
	}

	//处理完成工单
	public function order_remark(){
		$workorder_id = I('post.workorder_id',0,'intval');
		$remark = I('post.remark');
		if(empty($remark)){
			$this->error('备注不能为空');
		}
		$workorder_info = M('workorder')->where("id = $workorder_id")->find();
		if($workorder_info && ($workorder_info['department']==cookie('founder') || cookie('founder') == 1)){
			if($workorder_info['workorder_status'] != 2){
				$this->error('订单已备注');
			}
			$adminid = urlsafe_b64decode(cookie("heardadminData"));
			$save['workorder_status'] = 3;
			$save['handle_id'] = $adminid;
			$save['remark_time'] = time();
			$save['remark'] = $remark;
			$res = M('workorder')->where(['id'=>$workorder_id])->save($save);
			if($res){
				$this->adminlog("处理工单".$workorder_id);
				$this->success('处理成功','/Home/Workorder/workorder_list',1);
			}else{
				$this->error('处理失败');
			}
		}else{
			$this->error('工单不存在或无权处理该订单');
		}
	}

}